﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Util.Database;

namespace ConfigureNZBLoader
{
    public partial class EditDownloadRequests : Form
    {
        public EditDownloadRequests()
        {
            InitializeComponent();
        }

        private uint _ShowId;
        public uint ShowId
        {
            get { return _ShowId; }
            set { _ShowId = value; }
        }

        class RequestListAdapter
        {
            public uint season;
            public uint episode;
            public uint id;

            public RequestListAdapter(uint id, uint season, uint episode)
            {
                this.id = id;
                this.season = season;
                this.episode = episode;
            }

            public override string ToString()
            {
                if (season == 0 && episode == 0)
                {
                    return "All seasons/episodes";
                }
                else if (episode == 0 && season != 0)
                {
                    return string.Format(System.Globalization.CultureInfo.InvariantCulture, "Season {0}", season);
                }
                else
                {
                    return string.Format(System.Globalization.CultureInfo.InvariantCulture, "Season {0} Episode {1}", season, episode);
                }
            }
        }

        private void EditDownloadRequests_Load(object sender, EventArgs e)
        {
            using (IDbCommand cmd = DB.CreateCommand())
            {
                cmd.CommandText = "select * from request where id_show=? order by season, episode";
                IDataParameter par = cmd.CreateParameter();
                par.Value = ShowId;
                cmd.Parameters.Add(par);

                using (IDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        lstRequests.Items.Add(new RequestListAdapter((uint)(long)dr["ID"], (uint)(long)dr["SEASON"], (uint)(long)dr["EPISODE"]));
                    }
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddRequest dlg = new AddRequest();
            if (DialogResult.OK == dlg.ShowDialog(this))
            {
                try
                {
                    uint Season = uint.Parse(dlg.Season, System.Globalization.CultureInfo.InvariantCulture);
                    uint Episode = uint.Parse(dlg.Episode, System.Globalization.CultureInfo.InvariantCulture);
                    using (IDbCommand cmd = DB.CreateCommand())
                    {
                        cmd.CommandText = "select count(*) from downloaded where id_show=? and season=? and episode=?";
                        IDataParameter par = cmd.CreateParameter();
                        par.Value = ShowId;
                        cmd.Parameters.Add(par);
                        par = cmd.CreateParameter();
                        par.Value = Season;
                        cmd.Parameters.Add(par);
                        par = cmd.CreateParameter();
                        par.Value = Episode;
                        cmd.Parameters.Add(par);

                        if ((uint)(long)cmd.ExecuteScalar() != 0)
                        {
                            if(DialogResult.No == System.Windows.Forms.MessageBox.Show("It seems as if you already downloaded this episode. Do you really want to add it again?", "?", MessageBoxButtons.YesNo))
                            {
                                return;
                            }
                            cmd.CommandText = "delete from downloaded where id_show=? and season=? and episode=?";
                            cmd.ExecuteNonQuery();
                        }

                        cmd.CommandText = "insert into request(id_show, season, episode) values (?, ?, ?)";
                        cmd.ExecuteNonQuery();

                        cmd.Parameters.Clear();
                        cmd.CommandText = "select * from request where id = (select max(id) from request)";
                        using (IDataReader dr = cmd.ExecuteReader())
                        {
                            if (dr.Read())
                            {
                                lstRequests.Items.Add(new RequestListAdapter((uint)(long)dr["ID"], (uint)(long)dr["SEASON"], (uint)(long)dr["EPISODE"]));
                            }
                        }
                    }

                }
                catch (Exception)
                {
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            RequestListAdapter obj = (RequestListAdapter)lstRequests.SelectedItem;
            if (obj != null)
            {
                lstRequests.Items.Remove(obj);

                using (IDbCommand cmd = DB.CreateCommand())
                {
                    cmd.CommandText = "delete from request where id=?";
                    IDataParameter par = cmd.CreateParameter();
                    par.Value = obj.id;
                    cmd.Parameters.Add(par);
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}
